//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "GeneralStatistics.h"
using namespace Cephei::QL::Math::Statistics;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Statistics::CGeneralStatistics::CGeneralStatistics () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phGeneralStatistics = NULL;
#endif
        _ppGeneralStatistics = new boost::shared_ptr<QuantLib::GeneralStatistics> (new QuantLib::GeneralStatistics ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Statistics::CGeneralStatistics::CGeneralStatistics (boost::shared_ptr<QuantLib::GeneralStatistics>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGeneralStatistics = NULL;
#endif
	_ppGeneralStatistics = &childNative;
    
}
Cephei::QL::Math::Statistics::CGeneralStatistics::CGeneralStatistics (QuantLib::GeneralStatistics& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phGeneralStatistics = NULL;
#endif
	_ppGeneralStatistics = new boost::shared_ptr<QuantLib::GeneralStatistics> (&childNative);
    
    _GeneralStatisticsOwner = owner;
    
}

Cephei::QL::Math::Statistics::CGeneralStatistics::CGeneralStatistics (CGeneralStatistics^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGeneralStatistics = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppGeneralStatistics = new boost::shared_ptr<QuantLib::GeneralStatistics> (copy->GetShared());
        
    }
}
Cephei::QL::Math::Statistics::CGeneralStatistics::CGeneralStatistics (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGeneralStatistics = NULL;
#endif
	if (!t->IsSubclassOf(CGeneralStatistics::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Statistics::CGeneralStatistics::CGeneralStatistics (QuantLib::Handle<QuantLib::GeneralStatistics>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGeneralStatistics = &childNative;
	_ppGeneralStatistics = &static_cast<boost::shared_ptr<QuantLib::GeneralStatistics>>(childNative.currentLink());
    
    _GeneralStatisticsOwner = owner;
}
Cephei::QL::Math::Statistics::CGeneralStatistics::CGeneralStatistics (QuantLib::Handle<QuantLib::GeneralStatistics> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phGeneralStatistics = &childNative;
	_ppGeneralStatistics = &static_cast<boost::shared_ptr<QuantLib::GeneralStatistics>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Statistics::CGeneralStatistics::CGeneralStatistics (QuantLib::GeneralStatistics childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phGeneralStatistics = NULL;
#endif
	_ppGeneralStatistics = new boost::shared_ptr<QuantLib::GeneralStatistics> (new QuantLib::GeneralStatistics (childNative));
    
}
#endif

Cephei::QL::Math::Statistics::CGeneralStatistics::~CGeneralStatistics ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppGeneralStatistics != NULL)
    {
	    delete _ppGeneralStatistics;
        _ppGeneralStatistics = NULL;
    }
}
Cephei::QL::Math::Statistics::CGeneralStatistics::!CGeneralStatistics ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppGeneralStatistics != NULL)
    {
	    delete _ppGeneralStatistics;
    }
}
QuantLib::GeneralStatistics& Cephei::QL::Math::Statistics::CGeneralStatistics::GetReference ()
{
    if (_ppGeneralStatistics == NULL) throw gcnew NativeNullException ();
	return **_ppGeneralStatistics;
}
boost::shared_ptr<QuantLib::GeneralStatistics>& Cephei::QL::Math::Statistics::CGeneralStatistics::GetShared ()
{
    if (_ppGeneralStatistics == NULL) throw gcnew NativeNullException ();
	return *_ppGeneralStatistics;
}
QuantLib::GeneralStatistics* Cephei::QL::Math::Statistics::CGeneralStatistics::GetPointer ()
{
    if (_ppGeneralStatistics == NULL) throw gcnew NativeNullException ();
	return &**_ppGeneralStatistics;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::GeneralStatistics>& Cephei::QL::Math::Statistics::CGeneralStatistics::GetHandle ()
{
	if (_phGeneralStatistics == NULL)
	{
		_phGeneralStatistics = new Handle<QuantLib::GeneralStatistics> (*_ppGeneralStatistics);
	}
	return *_phGeneralStatistics;
}
#endif
bool Cephei::QL::Math::Statistics::CGeneralStatistics::HasNative () 
{
	return (_ppGeneralStatistics != NULL);
}

Cephei::QL::Math::Statistics::IGeneralStatistics^ Cephei::QL::Math::Statistics::CGeneralStatistics::Add (Double value, Microsoft::FSharp::Core::FSharpOption<Double>^ weight)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Real _value = (QuantLib::Real)ValueHelper::Convert (value);
        QuantLib::Real _weight = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (weight) ? (QuantLib::Real)ValueHelper::Convert (weight->Value) : 1.0); //9a
    	(*_ppGeneralStatistics)->add ( _value,  _weight );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::ErrorEstimate::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->errorEstimate ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::Kurtosis::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->kurtosis ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::Max::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->max ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::Mean::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->mean ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::Min::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->min ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::Percentile (Double y)
{
    try
    {
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->percentile ( _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Statistics::IGeneralStatistics^ Cephei::QL::Math::Statistics::CGeneralStatistics::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppGeneralStatistics)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Math::Statistics::CGeneralStatistics::Samples::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppGeneralStatistics)->samples ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::Skewness::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->skewness ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Statistics::IGeneralStatistics^ Cephei::QL::Math::Statistics::CGeneralStatistics::Sort::get ()
{
    try
    {
    	(*_ppGeneralStatistics)->sort ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::StandardDeviation::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->standardDeviation ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::TopPercentile (Double y)
{
    try
    {
        QuantLib::Real _y = (QuantLib::Real)ValueHelper::Convert (y);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->topPercentile ( _y );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::Variance::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->variance ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Math::Statistics::CGeneralStatistics::WeightSum::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppGeneralStatistics)->weightSum ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Statistics::IGeneralStatistics^ Cephei::QL::Math::Statistics::CGeneralStatistics_Factory::Create ()
{
    return gcnew CGeneralStatistics ();
}
